package com.bbcare.nutritious.plat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.QueryModel;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.ToolUtils;
import com.bbcare.nutritious.plat.service.INutritionSchemeUsedService;

/**
 *
 * @name:         NutritionSchemeUsedController.java
 * @author:       tao
 * @createTime:   2017年7月3日
 * @description:  营养方案使用 控制类
 * @modify:
 *
 */
@Controller
@RequestMapping("/service/nutrition")
public class NutritionSchemeUsedController {

    public static final Logger logger = Logger.getLogger(NutritionSchemeUsedController.class);

    @Autowired
    private INutritionSchemeUsedService schemeUsedService;

    //营养方案使用
    @ResponseBody
    @RequestMapping("/patientscheme/used")
    public ResultModel<Object> patientSchemeUsed(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientSchemeUsed----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String schemeVersionId = requestParam.getString("schemeVersionId");
        if(StringUtils.isEmpty(schemeVersionId)){
        	logger.debug("out patientSchemeUsed----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，方案ID不能为空");
        }

        String userId = requestParam.getString("userId");
        if(StringUtils.isEmpty(userId)){
        	logger.debug("out patientSchemeUsed----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，用户ID不能为空");
        }

        String useType = requestParam.getString("useType");

        /*String stages = requestParam.getString("stages");
        if(StringUtils.isEmpty(stages)){
        	logger.debug("out schemeAdd----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，方案阶段不能为空");
        }*/

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
//        String deptId = (String) sessionMap.get("deptId");// 科室id
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        Map<String,Object> rspMap = null;
        if(!StringUtils.isEmpty(useType) && "1".equals(useType)){
        	rspMap = schemeUsedService.patientSchemeUsed(requestParam);
        }else{
        	rspMap = schemeUsedService.patientSchemeUsedCache(requestParam);
        }

        logger.debug("out patientSchemeUsed----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }

    //用户当前营养方案查询(查询到阶段和疗程列表 ，疗程详情在疗程详情接口查询)
    @ResponseBody
    @RequestMapping("/patientscheme/detail")
    public ResultModel<Object> patientSchemeDetail(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientSchemeDetail----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String userId = requestParam.getString("userId");
        if(StringUtils.isEmpty(userId)){
        	logger.debug("out patientSchemeUsed----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，用户ID不能为空");
        }

        String ptSchemeId = requestParam.getString("ptSchemeId");
        if(StringUtils.isEmpty(ptSchemeId)){
        	logger.debug("out patientSchemeUsed----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，应用方案ID不能为空");
        }


        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
//        String deptId = (String) sessionMap.get("deptId");// 科室id
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("userId",userId);
        Map<String,Object> rspMap = schemeUsedService.patientSchemeDetail(requestParam);

        logger.debug("out patientSchemeDetail----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }

    //用户当前营养方案查询
    @ResponseBody
    @RequestMapping("/patientcourse/detail")
    public ResultModel<Object> patientCourseDetail(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientCourseDetail----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String ptCourseId = requestParam.getString("ptCourseId");
        if(StringUtils.isEmpty(ptCourseId)){
        	logger.debug("out patientCourseDetail----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，疗程ID不能为空");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
//        String deptId = (String) sessionMap.get("deptId");// 科室id
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("ptCourseId",ptCourseId);
        Map<String,Object> rspMap = schemeUsedService.patientCourseDetail(requestParam);

        logger.debug("out patientCourseDetail----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }

    /**
     * 查询所有的食物营养方案以及摄入量(包括不在计划内的食物)
     * @param request
     * @param jsonStr
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/patientCourseDetailForAllFood")
    public ResultModel<Object> patientCourseDetailForAllFood(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientCourseDetailForAllFood----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String ptCourseId = requestParam.getString("ptCourseId");
        if(StringUtils.isEmpty(ptCourseId)){
        	logger.debug("out patientCourseDetailForAllFood----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，疗程ID不能为空");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
//        String deptId = (String) sessionMap.get("deptId");// 科室id
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("ptCourseId",ptCourseId);
        Map<String,Object> rspMap = schemeUsedService.patientCourseDetailForAllFood(requestParam);

        logger.debug("out patientCourseDetailForAllFood----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }
    //设置疗程开始日期
    @ResponseBody
    @RequestMapping("/patientcourse/startdayset")
    public ResultModel<Object> patientCourseStartDaySet(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientCourseStartDaySet----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String ptCourseId = requestParam.getString("ptCourseId");
        if(StringUtils.isEmpty(ptCourseId)){
        	logger.debug("out patientCourseStartDaySet----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，疗程ID不能为空");
        }

        String startDate = requestParam.getString("startDate");
        if(StringUtils.isEmpty(startDate)){
        	logger.debug("out patientCourseStartDaySet----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，疗程开始日期不能为空");
        }

        //开始日期格式 不正确
        try{
        	DateUtil.getDate_8(startDate);
        }catch(Exception e){
        	logger.debug("out patientCourseStartDaySet----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，疗程开始日期格式不正确");
        }

        String ptSchemeId = requestParam.getString("ptSchemeId");
        if(StringUtils.isEmpty(ptSchemeId)){
        	logger.debug("out patientCourseStartDaySet----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，应用方案ID不能为空");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
//        String deptId = (String) sessionMap.get("deptId");// 科室id
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("ptSchemeId",ptSchemeId);
        requestParam.put("ptCourseId",ptCourseId);
        requestParam.put("startDate",startDate);

        Map<String,Object> rspMap = schemeUsedService.patientCourseStartDaySet(requestParam);

        logger.debug("out patientCourseDetail----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }


    //医生修改饮食情况、修改默认使用量、登记患者实际食用量
    @ResponseBody
    @RequestMapping("/patientcourse/situation/modify")
    public ResultModel<Object> patientSituationModify(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientSituationModify----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String situationIds = requestParam.getString("situationIds");
        if(StringUtils.isEmpty(situationIds)){
        	logger.debug("out patientSituationModify----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，situationIds不能为空");
        }

        List<String> situationList = JSONObject.parseArray(situationIds,String.class);
        if(CollectionUtils.isEmpty(situationList)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，situationIds不能为空");
        }

        String amount = requestParam.getString("amount");
        if(!ToolUtils.isNumeric(amount)){
        	logger.debug("out patientSituationModify----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，修改值不能为空,并且为数字");
        }

        String type = requestParam.getString("type");//1、修改默认食用量  2、医生替患者登记实际食用量 3、用户登记食用量 //走app专用接口
        if(StringUtils.isEmpty(type)){
        	logger.debug("out patientSituationModify----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，修改类型不能为空");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id
        String authorName = (String) sessionMap.get("authorName");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("authorName",authorName);
        requestParam.put("type",type);
        requestParam.put("amount",amount);
        requestParam.put("situationList",situationList);
//        requestParam.put("startDate",startDate);

        Map<String,Object> rspMap = schemeUsedService.patientSituationModify(requestParam);

        logger.info("out patientCourseDetail----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }

    //医生修改饮食情况、修改默认使用量、登记患者实际食用量
    @ResponseBody
    @RequestMapping("/coursesituation/modifylist")
    public ResultModel<Object> patientSituationModifyHis(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientSituationModify----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String situationId = requestParam.getString("situationId");
        if(StringUtils.isEmpty(situationId)){
        	logger.debug("out patientSituationModify----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，situationIds不能为空");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("situationId",situationId);

        List<Map<String, Object>> rspMap = schemeUsedService.patientSituationModifyHis(requestParam);

        logger.info("out patientCourseDetail----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }


    //营养方案停止
    @ResponseBody
    @RequestMapping("/patientscheme/stop")
    public ResultModel<Object> patientSchemeStop(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientSchemeStop----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String ptSchemeId = requestParam.getString("ptSchemeId");
        if(StringUtils.isEmpty(ptSchemeId)){
        	logger.debug("out patientSchemeStop----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，方案ID不能为空");
        }

        String userId = requestParam.getString("userId");
        if(StringUtils.isEmpty(userId)){
        	logger.debug("out patientSchemeStop----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，用户ID不能为空");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("remark","停止营养方案");
        Map<String,Object> rspMap = schemeUsedService.patientSchemeStop(requestParam);

        logger.info("out patientSchemeStop----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }

    //营养阶段启用
    @ResponseBody
    @RequestMapping("/patientStage/used")
    public ResultModel<Object> patientStageUsed(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientStageUsed----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String ptSchemeId = requestParam.getString("ptSchemeId");
        if(StringUtils.isEmpty(ptSchemeId)){
        	logger.debug("out patientStageUsed----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，方案ID不能为空");
        }

        String ptStageId = requestParam.getString("ptStageId");
        if(StringUtils.isEmpty(ptStageId)){
        	logger.debug("out patientStageUsed----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，阶段ID不能为空");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("remark","启用阶段");
        Map<String,Object> rspMap = schemeUsedService.patientStageUsed(requestParam);

        logger.info("out patientStageUsed----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }


    //营养方案停止
    @ResponseBody
    @RequestMapping("/patientscheme/change")
    public ResultModel<Object> patientSchemeChange(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientSchemeChange----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String ptSchemeId = requestParam.getString("ptSchemeId");
        if(StringUtils.isEmpty(ptSchemeId)){
        	logger.debug("out patientSchemeChange----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，方案ID不能为空");
        }

        String userId = requestParam.getString("userId");
        if(StringUtils.isEmpty(userId)){
        	logger.debug("out patientSchemeChange----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，用户ID不能为空");
        }

        String schemeVersionId = requestParam.getString("schemeVersionId");
        if(StringUtils.isEmpty(schemeVersionId)){
        	logger.debug("out patientSchemeChange----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，新方案ID不能为空");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("remark","变更营养方案");

        Map<String,Object> rspMap = schemeUsedService.patientSchemeChange(requestParam);

        logger.info("out patientSchemeChange----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }

    @ResponseBody
    @RequestMapping("/patientscheme/list")
    public QueryModel<Map<String, Object>> getPatientschemeList(@RequestBody String arg,
            HttpServletRequest request) throws Exception {
    	logger.info("in getPatientschemeList----arg==" + arg);
        JSONObject jsonEsb = JSON.parseObject(arg);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.putAll(requestParam);

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String authorId = (String) sessionMap.get("authorId");// 租户id
        paramMap.put("tenantId",tenantId);
        paramMap.put("authorId",authorId);

        List<Map<String, Object>> list= schemeUsedService.patientSchemeList(paramMap);
        int total= 0;
        if(!CollectionUtils.isEmpty(list)){
        	total = schemeUsedService.patientSchemeCount(paramMap);;
        }
		logger.info("out getPatientschemeList----arg==" + list);
		return new QueryModel<Map<String,Object>>(list,total);
    }

    //方案列表查询
    @ResponseBody
    @RequestMapping("/scheme/querylist")
    public QueryModel<Map<String, Object>> schemeListQuery(@RequestBody String arg,
            HttpServletRequest request) throws Exception {
    	logger.info("in schemeListQuery----arg==" + arg);
        JSONObject jsonEsb = JSON.parseObject(arg);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}

        Map<String, Object> paramMap = new HashMap<String, Object>();
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String authorId = (String) sessionMap.get("authorId");// 租户id
        paramMap.put("tenantId", tenantId);
        paramMap.put("authorId", authorId);
        paramMap.putAll(requestParam);

        // 针对Datatable特殊处理
        String jsonargs = requestParam.getString(Constants.INF_INPUTPARAM_DATATABLE_FIELD);
        int draw = 0;
        if (!StringUtils.isEmpty(jsonargs)) {
            JSONObject jsonTable = (JSONObject) JSONObject.parse(jsonargs);
            draw = jsonTable.getIntValue("draw");
            paramMap.put("page", jsonTable.getIntValue("start"));
            paramMap.put("rows", jsonTable.getIntValue("length"));
        }

        List<Map<String, Object>> list= schemeUsedService.schemeListQry(paramMap);
        int total= 0;
        if(!CollectionUtils.isEmpty(list)){
        	total= schemeUsedService.CountSchemeListQry(paramMap);
        }
		logger.info("out schemeListQuery----总条数==" + draw);
		return new QueryModel<Map<String,Object>>(list,total);
    }

    //患者应用页面统计数据
    @ResponseBody
    @RequestMapping("/patientscheme/statistics")
    public ResultModel<Object> getStatisticsData(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in getStatisticsData----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            // 后续重写异常类
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参节点错误，非json格式！"));
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            logger.warn("in getStatisticsData----no data node...");
            // 前台必须传data节点，否则报错返回（后续重写异常类）
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String authorId = (String) sessionMap.get("authorId");// 租户id
//        String deptId = (String) sessionMap.get("rootGroupId");// 租户id

        paramMap.put("authorId", authorId);
        paramMap.put("tenantId", tenantId);
        Map<String, Object> rspMap = schemeUsedService.getStatisticsData(paramMap);

        return new ResultModel<Object>(rspMap);
    }

  //设置疗程开始日期
    @ResponseBody
    @RequestMapping("/patientCourse/stop")
    public ResultModel<Object> patientCourseStop(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientCourseStop----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String ptCourseId = requestParam.getString("ptCourseId");
        if(StringUtils.isEmpty(ptCourseId)){
        	logger.debug("out patientCourseStartDaySet----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，疗程ID不能为空");
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
//        String deptId = (String) sessionMap.get("deptId");// 科室id
        String authorId = (String) sessionMap.get("authorId");//登陆用户id
        String tenantId = (String) sessionMap.get("tenantId");//租户科室id

        requestParam.put("tenantId",tenantId);
        requestParam.put("authorId",authorId);
        requestParam.put("ptCourseId",ptCourseId);

        Map<String,Object> rspMap = schemeUsedService.patientCourseStop(requestParam);

        logger.info("out patientCourseDetail----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }

}
